package com.lujianfei.switchtheme

import android.content.Intent
import android.os.Bundle
import android.util.TypedValue
import androidx.annotation.InspectableProperty
import com.lujianfei.library_switch_theme.callback.SkinUpdateListener
import com.lujianfei.switchtheme.base.BaseActivity
import com.lujianfei.switchtheme.utils.FileHelper
import com.lujianfei.switchtheme.utils.SPRepository
import com.lujianfei.switchtheme.utils.SkinManager
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File

class MainActivity : BaseActivity(),SkinUpdateListener{

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        SkinManager.INSTANCE.addSkinUpdateListener(this)
        setContentView(R.layout.activity_main)
        FileHelper.copySkinToDataDir()
        setSupportActionBar(toolbar)
        initView()
        switch_theme.setOnClickListener {
            SkinManager.INSTANCE.loadSkin(File(filesDir,Constant.Skin.SKIN1).absolutePath)
        }
        restore_theme.setOnClickListener {
            SkinManager.INSTANCE.restoreDefaultTheme()
        }
        new_activity.setOnClickListener {
            startActivity(Intent(this,NewActivity::class.java))
        }
    }

    private fun initView() {
        onSkinUpdate()
    }

    override fun onSkinUpdate() {
        SkinManager.INSTANCE.getColor(R.color.default_text_color)?.let {
            appname.setTextColor(it)
            switch_theme.setTextColor(it)
            restore_theme.setTextColor(it)
            new_activity.setTextColor(it)
        }
        SkinManager.INSTANCE.getDimension(R.dimen.default_textsize)?.let {
            appname.setTextSize(TypedValue.COMPLEX_UNIT_PX,it)
            switch_theme.setTextSize(TypedValue.COMPLEX_UNIT_PX,it)
            restore_theme.setTextSize(TypedValue.COMPLEX_UNIT_PX,it)
            new_activity.setTextSize(TypedValue.COMPLEX_UNIT_PX,it)
        }
        SkinManager.INSTANCE.getDrawable(R.drawable.selector_btn)?.let {
            switch_theme.background = it
        }
        SkinManager.INSTANCE.getDrawable(R.drawable.selector_btn)?.let {
            restore_theme.background = it
        }
        SkinManager.INSTANCE.getDrawable(R.drawable.selector_btn)?.let {
            new_activity.background = it
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        SkinManager.INSTANCE.removeSkinUpdateListener(this)
    }
}
